package com.dx.mes.service.special.service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import com.dx.common.table.ppt.*;
import com.dx.mes.common.dao.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.mes.common.table.rtnBean.NotFoundRtnBeen;
import com.dx.mes.common.table.rtnBean.SimpleRtnBeen;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.GUIDGenerator;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.special.service.IFppforceinService;
import com.dx.mes.facade.special.tx.Fppforcein.FppforceinI;
import com.dx.mes.facade.special.tx.Fppforcein.FppforceinIA;
import com.dx.mes.facade.special.tx.Fppforcein.FppforceinO;

import static com.dx.mes.common.bean.def.GenericDef.*;

@Service("fppforceinService")
public class FppforceinService implements IFppforceinService {
	private static final String TX_NAME = T_FPPFORCEIN;
	private LogUtils logUtils;
	@Autowired
	private IRetBoxDao retBoxDao;
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	@Autowired
	private IBisPathSwhDao bisPathSwhDao;
	@Autowired
	private IBisPathDao bisPathDao;
	@Autowired
	private IRetPrdOutDao retPrdOutDao;
	@Autowired
	private IBisPathiDao bisPathiDao;
	@Autowired
	private IBisOpeDao    bisOpeDao;

	@Override
	public String subMainProc(String evt_no, String strInTrx) {
		AppContext.clear();
		AppContext.setCurrServiceName(FppforceinService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FppforceinService.class);
		logUtils.info("[InTrx:" + strInTrx + "]");
		String strOutTrx = "";
		FppforceinI inTrx = new FppforceinI();
		FppforceinO outTrx = new FppforceinO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

		try {
			inTrx = JacksonUtil.fromJson(strInTrx, FppforceinI.class);
			char action_flg = inTrx.getAction_flg().charAt(0);
			switch (action_flg) {
				case 'R':
					repairFnc(inTrx, outTrx);
					break;
				case 'C':
					cancelRepairFnc(inTrx, outTrx);
					break;
				case 'F':
					forceRepairFnc(inTrx, outTrx);
					break;
				case 'A':
					abortRepairFnc(inTrx, outTrx);
					break;
				default:
					errActionFnc(outTrx);
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				retBoxDao.flushCurSession();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		} catch (Exception ex) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			strOutTrx = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + strOutTrx + "]");
		}
		return strOutTrx;
	}

	public void repairFnc(FppforceinI inTrx, FppforceinO outTrx) {
		/*List<FppforceinIA> iary = inTrx.getIary();
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen();
		String boxId = iary.get(0).getBox_id();
		String swhId = iary.get(0).getSwh_id();
		String swhTyp = iary.get(0).getSwh_typ();
		String swhPathIdFk = iary.get(0).getSwh_path_id_fk();
		String swhPathVerk	= iary.get(0).getSwh_path_ver_fk();
		String crPathIdFk = iary.get(0).getCr_path_id_fk();
		String crPathVerFk = iary.get(0).getCr_path_ver_fk();
		String crOpeNoFk = iary.get(0).getCr_ope_no_fk();
		String outPathIdFk = iary.get(0).getOut_path_id_fk();
		String outPathVerFk = iary.get(0).getOut_path_ver_fk();
		String outOpeNoFk  = iary.get(0).getOut_ope_no_fk();
		String rsnCate = iary.get(0).getRsn_cate();
		String rsnCode = iary.get(0).getRsn_code();
		String rsnDept = iary.get(0).getRsn_dept();
		String evtUsr  = iary.get(0).getEvt_usr();
		List<Ret_prd_info> ret_prd_infos =  iary.get(0).getRet_prd_info();
		Ret_box ret_box = retBoxDao.get(boxId);
		String evtCate = null;
		if("R".equals(swhTyp)){
			evtCate=EVT_REPAIR;
		}else if("K".equals(swhTyp)){
			evtCate=EVT_REWORK;
		}else{
			evtCate=EVT_OQC;
		}
		Bis_path bis_path_to = null;
		Bis_ope bis_ope_to = null;
		//根据分支途程和版本找出该分支途程的第一站
		String sql = " select * FROM Bis_path WHERE path_id_fk='" + swhPathIdFk + "' AND path_ver_fk='" + swhPathVerk + "'";
		List<Bis_path> swhBis_Paths = bisPathiDao.findBySQL(sql,Bis_path.class);
		String toOpeNoFk="";
		for(Bis_path bis_path : swhBis_Paths){
			if("FRST".equals(bis_path.getPv_ope_no())){
				toOpeNoFk=bis_path.getId().getCr_ope_no();
			}
		}
		checkPrdWait(boxId,outTrx);//检查玻璃是否Wait
		if(outTrx.getRtn_mesg()!=null){
			return;
		}
		checkBoxPrdPath(boxId,outTrx);//判断箱子的NX_PATH_ID&VER信息是否维护NX_OPE_ID(bis_ope)信息是否维护
		if(outTrx.getRtn_mesg()!=null){
			return;
		}
		rtnBeen = checkBoxSwhPath(swhPathIdFk,swhPathVerk,toOpeNoFk,outPathIdFk,outPathVerFk,outOpeNoFk,outTrx, bis_path_to, bis_ope_to);
		if(rtnBeen.getlRc()!= _NORMAL){
			outTrx.setRtn_code( String.valueOf(rtnBeen.getlRc()) );
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return;
		}
		bis_ope_to = (Bis_ope) rtnBeen.getObj();
		*//*updatePrd(boxId,evtUsr,swhPathIdFk,swhPathVerk,toOpeNoFk,rsnCate,rsnCode,rsnDept,evtCate, bis_ope_to,ret_prd_infos, outTrx);//更新玻璃代码
		if(outTrx.getRtn_code()!=RETURN_CODE_OK){
			return;
		}*//*
		List<Ret_prd_info> ret_prd_infoList = retPrdInfoDao.getShtListInfo(boxId);//prd_out记录
		for (Ret_prd_info ret_prd_info : ret_prd_infoList) {
			//此处判断要保存的数据是否重复
			checkPrdRepeat(ret_prd_info.getPrd_seq_id(),swhTyp,crPathIdFk,crPathVerFk,toOpeNoFk,swhPathIdFk,swhPathVerk,outTrx);//检查Box是否Wait
			if(outTrx.getRtn_code() != RETURN_CODE_OK){
				return;
			}
			Ret_prd_outId ret_prd_outId = new Ret_prd_outId();
			ret_prd_outId.setEvt_seq_id_fk(GUIDGenerator.javaGUID());
			ret_prd_outId.setPrd_seq_id_fk(ret_prd_info.getPrd_seq_id());// 玻璃id
			Timestamp evt_timestamp = Util.getCurrentTimestamp();
			ret_prd_outId.setEvt_timestamp(evt_timestamp);
			Ret_prd_out ret_prd_out = new Ret_prd_out();
			ret_prd_out.setId(ret_prd_outId);
			ret_prd_out.setUnq_seq_id(GUIDGenerator.javaGUID());
			ret_prd_out.setFr_path_id_fk(crPathIdFk);
			ret_prd_out.setFr_path_ver_fk(crPathVerFk);
			ret_prd_out.setFr_ope_no_fk(crOpeNoFk);
			ret_prd_out.setSwh_id(swhId);
			ret_prd_out.setSwh_typ(swhTyp);
			ret_prd_out.setTo_path_id_fk(swhPathIdFk);
			ret_prd_out.setTo_path_ver_fk(swhPathVerk);
			ret_prd_out.setTo_ope_no_fk(toOpeNoFk);
			ret_prd_out.setOut_path_id_fk(outPathIdFk);
			ret_prd_out.setOut_path_ver_fk(outPathVerFk);
			ret_prd_out.setOut_ope_no_fk(outOpeNoFk);
			ret_prd_out.setRsn_cate(rsnCate);
			ret_prd_out.setRsn_code(rsnCode);
			ret_prd_out.setRsn_dept(rsnDept);
			ret_prd_out.setEvt_usr(evtUsr);
			retPrdOutDao.save(ret_prd_out);// 增加修补记录
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		String returnMesgOk = RETURN_MESG_OK;
		outTrx.setRtn_mesg(returnMesgOk);*/
	}
	public void cancelRepairFnc(FppforceinI inTrx, FppforceinO outTrx) {
		List<FppforceinIA> iary = inTrx.getIary();
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen();
		String swh_typ =iary.get(0).getSwh_typ();//前台传值
		String rsnCate = iary.get(0).getRsn_cate();
		String rsnCode = iary.get(0).getRsn_code();
		String rsnDept = iary.get(0).getRsn_dept();
		String evtUsr  = iary.get(0).getEvt_usr();
		List<Ret_prd_info> ret_prd_infos =  iary.get(0).getRet_prd_info();
		List<Ret_prd_info> ret_prd_infoList = new ArrayList<>();
		for(Ret_prd_info ret_prd_info :ret_prd_infos){
			Ret_prd_info ret_prd_infoA = retPrdInfoDao.get(ret_prd_info.getPrd_seq_id());
			ret_prd_infoList.add(ret_prd_infoA);
		}
		// 1.判断箱子和玻璃是不是在修补路程上或者重工途程上
		// 2判断箱子是不是wait在修补进程或者重工途程上的第一站
		// 3取消箱子和玻璃的修补进程或者重工途程上
		String sql = " SELECT * FROM ret_prd_out WHERE 1=1";
		if (!Util.isSpaceCheck(ret_prd_infoList.get(0).getPrd_seq_id())) {
			sql += " AND prd_seq_id_fk='" + ret_prd_infoList.get(0).getPrd_seq_id() + "'";
		}
		if (!Util.isSpaceCheck(ret_prd_infoList.get(0).getSv_path_id_fk())) {
			sql += " AND fr_path_id_fk='" + ret_prd_infoList.get(0).getSv_path_id_fk() + "'";
		}
		if (!Util.isSpaceCheck(ret_prd_infoList.get(0).getSv_path_ver_fk())) {
			sql += " AND fr_path_ver_fk='" + ret_prd_infoList.get(0).getSv_path_ver_fk() + "'";
		}
		if (!Util.isSpaceCheck(ret_prd_infoList.get(0).getSv_ope_no_fk())) {
			sql += " AND fr_ope_no_fk='" + ret_prd_infoList.get(0).getSv_ope_no_fk() + "'";
		}
		if (!Util.isSpaceCheck(ret_prd_infoList.get(0).getNx_path_id_fk())) {
			sql += " AND to_path_id_fk='" + ret_prd_infoList.get(0).getNx_path_id_fk() + "'";
		}
		if (!Util.isSpaceCheck(ret_prd_infoList.get(0).getNx_path_ver_fk())) {
			sql += " AND to_path_ver_fk='" + ret_prd_infoList.get(0).getNx_path_ver_fk() + "'";
		}
		List<Ret_prd_out> ret_prd_outs = retPrdOutDao.findBySQL(sql, Ret_prd_out.class);
		if (ret_prd_outs.size() <= 0) {
			outTrx.setRtn_mesg("屏体不在重工途程上");
			outTrx.setRtn_code(E_FPPFORCEIN_BOX_SWH_TYP_ERROR+"");
			return;
		}
		Bis_pathId  id = new Bis_pathId();
		id.setPath_ver_fk(ret_prd_infoList.get(0).getNx_path_ver_fk());
		id.setPath_id_fk(ret_prd_infoList.get(0).getNx_path_id_fk());
		id.setCr_ope_no(ret_prd_infoList.get(0).getNx_ope_no_fk());
		Bis_path   bis_path = bisPathDao.get(id);
		if (bis_path!=null) {
			if (bis_path.getPv_ope_no() != null && !"FRST".equals(bis_path.getPv_ope_no())) {// 第一站
				outTrx.setRtn_mesg("屏体不在重工途程的第一站，请选择终止重工操作");
				outTrx.setRtn_code(E_FPPFORCEIN_OPE_NOT_FIRST +"");
				return;
			}
			Bis_pathiId  bis_pathiId = new Bis_pathiId();  //查出分支路线
			bis_pathiId.setPath_id(ret_prd_infoList.get(0).getNx_path_id_fk());
			bis_pathiId.setPath_ver(ret_prd_infoList.get(0).getNx_path_ver_fk());
			Bis_pathi   bisPathi  = bisPathiDao.get(bis_pathiId);
			Timestamp evt_timestamp = Util.getCurrentTimestamp();
			for (Ret_prd_info ret_prd_info : ret_prd_infoList) {
				ret_prd_info.setNx_path_id_fk(ret_prd_info.getSv_path_id_fk());
				ret_prd_info.setNx_path_ver_fk(ret_prd_info.getSv_path_ver_fk());
				ret_prd_info.setNx_ope_no_fk(ret_prd_info.getSv_ope_no_fk());
				ret_prd_info.setNx_ope_id_fk(ret_prd_info.getSv_ope_id_fk());
				ret_prd_info.setNx_ope_ver_fk(ret_prd_info.getSv_ope_ver_fk());
				ret_prd_info.setNx_proc_id_fk(ret_prd_info.getSv_proc_id_fk());
				ret_prd_info.setNx_tool_run_mode(ret_prd_info.getSv_tool_run_mode());
				ret_prd_info.setSv_path_id_fk(_SPACE);
				ret_prd_info.setSv_path_ver_fk(_SPACE);
				ret_prd_info.setSv_ope_no_fk(_SPACE);
				ret_prd_info.setSv_ope_id_fk(_SPACE);
				ret_prd_info.setSv_ope_ver_fk(_SPACE);
				ret_prd_info.setSv_proc_id_fk(_SPACE);
				ret_prd_info.setSv_tool_run_mode(_SPACE);
				ret_prd_info.setRsn_cate(rsnCate);
				ret_prd_info.setRsn_code(rsnCode);
				ret_prd_info.setRsn_dept(rsnDept);
				ret_prd_info.setEvt_timestamp(evt_timestamp);
				ret_prd_info.setEvt_usr(evtUsr);
				ret_prd_info.setEvt_cate(EVT_REWORK_CANCEL);//
				ret_prd_info.setProc_flg("Y");//TODO:不知道这样赋值有没有问题
				retPrdInfoDao.update(ret_prd_info);// 更新玻璃信息
			}
			rtnBeen = deleteRetPrdOutProc(ret_prd_infoList,swh_typ,bisPathi);//删除out信息
			if(rtnBeen.getlRc()!= _NORMAL){
				outTrx.setRtn_code( String.valueOf(rtnBeen.getlRc()) );
				outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
				return;
			}
		}else {
			outTrx.setRtn_mesg("屏体的修补途程不存在！！！[BIS_PATH]");
			outTrx.setRtn_code(E_BIS_PATH +E_READ_NOT_FOUND +"");
			return;
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	public void forceRepairFnc(FppforceinI inTrx, FppforceinO outTrx) {
		List<FppforceinIA> iary = inTrx.getIary();
		String nx_path_id_fk = iary.get(0).getNx_path_id_fk();
		String nx_path_ver_fk= iary.get(0).getNx_path_ver_fk();
		String nx_ope_no_fk  =  iary.get(0).getNx_ope_no_fk();
		String rePathIdFk = iary.get(0).getRe_path_id_fk();
		String rePathVerFk =  iary.get(0).getRe_path_ver_fk();
		String rsnCate = iary.get(0).getRsn_cate();
		String rsnCode = iary.get(0).getRsn_code();
		String rsnDept = iary.get(0).getRsn_dept();
		String evtUsr  = iary.get(0).getEvt_usr();
		List<Ret_prd_info> ret_prd_infos =  iary.get(0).getRet_prd_info();
		List<Ret_prd_info> ret_prd_infoList = new ArrayList<>();
		for(Ret_prd_info ret_prd_info :ret_prd_infos){
			Ret_prd_info ret_prd_infoA = retPrdInfoDao.get(ret_prd_info.getPrd_seq_id());
			if (BANK_IN.equals(ret_prd_info.getBnk_flg())) {
				outTrx.setRtn_mesg("屏体代码[" + ret_prd_infoA.getPrd_seq_id() + "]在在制仓内，无法跳站");
				outTrx.setRtn_code(E_FPPFORCEIN_PRD_STATUE_ERROR +"");
				return ;
			}
			ret_prd_infoList.add(ret_prd_infoA);
		}
		String evtCate = EVT_REWORK;
		Bis_ope bis_ope_to = null;
		Bis_opeId to_ope_key = new Bis_opeId();

		//判断分支工艺路线是否存在
		Bis_pathi bis_pathi = new Bis_pathi();
		Bis_pathiId id = new Bis_pathiId();
		id.setPath_id(rePathIdFk);
		id.setPath_ver(rePathVerFk);
		bis_pathi = bisPathiDao.get(id);
		if(bis_pathi == null){
			outTrx.setRtn_mesg("该工艺路线["+rePathIdFk+"]["+rePathVerFk+"]不存在");
			outTrx.setRtn_code(E_BIS_PATHI + E_READ_NOT_FOUND+"");
			return;
		}
		//根据分支途程和版本找出该分支途程的第一站
		String sql = " select * FROM Bis_path WHERE path_id_fk='" + rePathIdFk + "' AND path_ver_fk='" + rePathVerFk + "'";
		List<Bis_path> swhBis_Paths = bisPathiDao.findBySQL(sql,Bis_path.class);
		String toOpeNoFk="";
		for(Bis_path bis_path : swhBis_Paths){
			if("FRST".equals(bis_path.getPv_ope_no())){
				toOpeNoFk=bis_path.getId().getCr_ope_no();
			}
		}
		checkPrd(ret_prd_infoList,outTrx);//检查卡匣中的玻璃有没有混装
		if(outTrx.getRtn_mesg()!=null){
			return;
		}
		//get to ope information
		Bis_pathId path_key = new Bis_pathId();
		path_key.setPath_id_fk(rePathIdFk);
		path_key.setPath_ver_fk(rePathVerFk);
		path_key.setCr_ope_no(toOpeNoFk);
		Bis_path to_bis_path = bisPathDao.get(path_key);
		to_ope_key.setOpe_id(to_bis_path.getCr_ope_id_fk());
		to_ope_key.setOpe_ver(to_bis_path.getCr_ope_ver_fk());
		bis_ope_to = bisOpeDao.get(to_ope_key);


		//set out information
		String out_path_id = ret_prd_infoList.get(0).getNx_path_id_fk();
		String out_path_ver = ret_prd_infoList.get(0).getNx_path_ver_fk();
		String out_ope_no = ret_prd_infoList.get(0).getNx_ope_no_fk();

		updatePrd(ret_prd_infoList, evtUsr, rePathIdFk, rePathVerFk, toOpeNoFk,
				rsnCate, rsnCode, rsnDept, evtCate, bis_ope_to,ret_prd_infos, outTrx);//更新玻璃代码
		if(outTrx.getRtn_code()!=RETURN_CODE_OK){
			return;
		}
		for (Ret_prd_info ret_prd_info : ret_prd_infoList) {
			//此处判断要保存的数据是否重复
			checkPrdRepeat(ret_prd_info.getPrd_seq_id(), nx_path_id_fk, nx_path_ver_fk,
					nx_ope_no_fk, rePathIdFk, rePathVerFk, outTrx);
			if(outTrx.getRtn_code() != RETURN_CODE_OK){
				return;
			}
			Ret_prd_outId ret_prd_outId = new Ret_prd_outId();
			ret_prd_outId.setEvt_seq_id_fk(GUIDGenerator.javaGUID());
			ret_prd_outId.setPrd_seq_id_fk(ret_prd_info.getPrd_seq_id());// 玻璃id
			Timestamp evt_timestamp = Util.getCurrentTimestamp();
			ret_prd_outId.setEvt_timestamp(evt_timestamp);
			Ret_prd_out ret_prd_out = new Ret_prd_out();
			ret_prd_out.setId(ret_prd_outId);
			ret_prd_out.setUnq_seq_id(GUIDGenerator.javaGUID());
			ret_prd_out.setSwh_typ("K");
			ret_prd_out.setFr_path_id_fk(nx_path_id_fk);
			ret_prd_out.setFr_path_ver_fk(nx_path_ver_fk);
			ret_prd_out.setFr_ope_no_fk(nx_ope_no_fk);
			ret_prd_out.setTo_path_id_fk(rePathIdFk);
			ret_prd_out.setTo_path_ver_fk(rePathVerFk);
			ret_prd_out.setTo_ope_no_fk(toOpeNoFk);
			ret_prd_out.setOut_path_id_fk(out_path_id);//未更新前的信息
			ret_prd_out.setOut_path_ver_fk(out_path_ver);//未更新前的信息
			ret_prd_out.setOut_ope_no_fk(out_ope_no);
			ret_prd_out.setRsn_cate(rsnCate);
			ret_prd_out.setRsn_code(rsnCode);
			ret_prd_out.setRsn_dept(rsnDept);
			ret_prd_out.setEvt_usr(evtUsr);
			retPrdOutDao.save(ret_prd_out);// 增加修补记录
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}
	public void abortRepairFnc(FppforceinI inTrx, FppforceinO outTrx) {
		List<FppforceinIA> iary = inTrx.getIary();
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen();
		String swh_typ =iary.get(0).getSwh_typ();//前台传值
		String rsnCate = iary.get(0).getRsn_cate();
		String rsnCode = iary.get(0).getRsn_code();
		String rsnDept = iary.get(0).getRsn_dept();
		String evtUsr  = iary.get(0).getEvt_usr();
		List<Ret_prd_info> ret_prd_infos =  iary.get(0).getRet_prd_info();
		List<Ret_prd_info> ret_prd_infoList = new ArrayList<>();
		for(Ret_prd_info ret_prd_info :ret_prd_infos){
			Ret_prd_info ret_prd_infoA = retPrdInfoDao.get(ret_prd_info.getPrd_seq_id());
			ret_prd_infoList.add(ret_prd_infoA);
		}
		String sql = " SELECT * FROM ret_prd_out WHERE 1=1";
		if (!Util.isSpaceCheck(ret_prd_infoList.get(0).getPrd_seq_id())) {
			sql += " AND prd_seq_id_fk='" + ret_prd_infoList.get(0).getPrd_seq_id() + "'";
		}
		if (!Util.isSpaceCheck(ret_prd_infoList.get(0).getSv_path_id_fk())) {
			sql += " AND fr_path_id_fk='" + ret_prd_infoList.get(0).getSv_path_id_fk() + "'";
		}
		if (!Util.isSpaceCheck(ret_prd_infoList.get(0).getSv_path_ver_fk())) {
			sql += " AND fr_path_ver_fk='" + ret_prd_infoList.get(0).getSv_path_ver_fk() + "'";
		}
		if (!Util.isSpaceCheck(ret_prd_infoList.get(0).getSv_ope_no_fk())) {
			sql += " AND fr_ope_no_fk='" + ret_prd_infoList.get(0).getSv_ope_no_fk() + "'";
		}
		if (!Util.isSpaceCheck(ret_prd_infoList.get(0).getNx_path_id_fk())) {
			sql += " AND to_path_id_fk='" + ret_prd_infoList.get(0).getNx_path_id_fk() + "'";
		}
		if (!Util.isSpaceCheck(ret_prd_infoList.get(0).getNx_path_ver_fk())) {
			sql += " AND to_path_ver_fk='" + ret_prd_infoList.get(0).getNx_path_ver_fk() + "'";
		}
		List<Ret_prd_out> ret_prd_outs = retPrdOutDao.findBySQL(sql, Ret_prd_out.class);
		if (ret_prd_outs.size() <= 0) {
			outTrx.setRtn_mesg("屏体不在重工途程上");
			outTrx.setRtn_code(E_FPPFORCEIN_BOX_SWH_TYP_ERROR+"");
			return;
		}
		Bis_pathId  id = new Bis_pathId();
		id.setPath_ver_fk(ret_prd_infoList.get(0).getNx_path_ver_fk());
		id.setPath_id_fk(ret_prd_infoList.get(0).getNx_path_id_fk());
		id.setCr_ope_no(ret_prd_infoList.get(0).getNx_ope_no_fk());
		Bis_path   bis_path = bisPathDao.get(id);
		if (bis_path!=null) {
			if (bis_path.getPv_ope_no() != null && "FRST".equals(bis_path.getPv_ope_no())) {// 第一站
					outTrx.setRtn_mesg("卡匣在重工途程的第一站，请选择取消重工操作");
					outTrx.setRtn_code(E_FPPFORCEIN_OPE_NOT_FIRST +"");
					return;
			}
			//终止重工时，屏体转入重工路线的最后一站 ：路线信息一样下一站站点opeNo为空
		    String hql = " FROM Bis_path WHERE path_id_fk='" + ret_prd_infoList.get(0).getNx_path_id_fk() + "' " +
					"and path_ver_fk='" + ret_prd_infoList.get(0).getNx_path_ver_fk() + "' and nx_ope_no is null ";
			List<Bis_path> lastPathList = bisPathDao.find(hql);
			Bis_path bisPath = null;
			if(lastPathList.size()>0){
				bisPath= lastPathList.get(0);
			}
			if(bisPath==null){
				outTrx.setRtn_mesg("重工途程的最后一站信息不存在");
				outTrx.setRtn_code(E_FPPFORCEIN_OPE_NOT_LAST +"");
				return;
			}

		    Timestamp evt_timestamp = Util.getCurrentTimestamp();

			for (Ret_prd_info ret_prd_info : ret_prd_infoList) {
				ret_prd_info.setNx_ope_no_fk(bisPath.getId().getCr_ope_no());
				ret_prd_info.setNx_ope_id_fk(bisPath.getCr_ope_id_fk());
				ret_prd_info.setNx_ope_ver_fk(bisPath.getCr_ope_ver_fk());
				ret_prd_info.setRsn_cate(rsnCate);
				ret_prd_info.setRsn_code(rsnCode);
				ret_prd_info.setRsn_dept(rsnDept);
				ret_prd_info.setEvt_timestamp(evt_timestamp);
				ret_prd_info.setEvt_usr(evtUsr);
				ret_prd_info.setEvt_cate(EVT_REWORK_ABORT);
				ret_prd_info.setProc_flg("Y");
				retPrdInfoDao.update(ret_prd_info);// 终止玻璃修补
			}
		}else{
			outTrx.setRtn_mesg("屏体的下一途程不存在！！！，[BIS_PATH]");
			outTrx.setRtn_code(E_BIS_PATH +E_READ_NOT_FOUND +"");
			return;
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
	}



	public void  updateBox(String boxId,String evtUsr,String toPathIdFk,String toPathVerFk,String toOpeNoFk,String rsnCate,
						   String rsnCode,String rsnDept,String eveCate,Bis_ope bis_ope_to, FppforceinO outTrx){
		Ret_box ret_box = retBoxDao.get(boxId);
		if (ret_box == null) {
			String gszMsgBuf = new NotFoundRtnBeen(boxId, Ret_box.class).toString();
			outTrx.setRtn_mesg(gszMsgBuf);
			outTrx.setRtn_code(String.valueOf(E_RET_BOX + E_READ_NOT_FOUND));
			return;
		}
		ret_box.setNx_path_id_fk(toPathIdFk);
		ret_box.setNx_path_ver_fk(toPathVerFk);
		ret_box.setNx_ope_no_fk(toOpeNoFk);
		ret_box.setNx_ope_id_fk(bis_ope_to.getId().getOpe_id());
		ret_box.setNx_ope_ver_fk(bis_ope_to.getId().getOpe_ver());
		ret_box.setNx_proc_id_fk(bis_ope_to.getProc_id());
		ret_box.setNx_tool_run_mode(bis_ope_to.getTool_run_mode());
		Timestamp evt_timestamp = Util.getCurrentTimestamp();
		ret_box.setEvt_timestamp(evt_timestamp);
		ret_box.setEvt_usr(evtUsr);
		ret_box.setEvt_cate(eveCate);//
		retBoxDao.update(ret_box);// 更新完箱子信息
		outTrx.setRtn_code(RETURN_CODE_OK);
	}
	public void  updatePrd(List<Ret_prd_info> ret_prd_infoList,String evtUsr,String toPathIdFk,String toPathVerFk,String toOpeNoFk,String rsnCate,String rsnCode,
						   String rsnDept,String eveCate,Bis_ope bis_ope_to,List<Ret_prd_info> ret_prd_infos, FppforceinO outTrx){
		if (ret_prd_infoList.get(0) == null) {
			outTrx.setRtn_mesg("数据不存在！！！,[Ret_prd_info]");
			outTrx.setRtn_code(String.valueOf(E_RET_BOX + E_READ_NOT_FOUND));
			return;
		}
		for(Ret_prd_info ret_prd_info : ret_prd_infoList){
			//存储历史信息
			ret_prd_info.setSv_path_id_fk(ret_prd_info.getNx_path_id_fk());
			ret_prd_info.setSv_path_ver_fk(ret_prd_info.getNx_path_ver_fk());
			ret_prd_info.setSv_ope_no_fk(ret_prd_info.getNx_ope_no_fk());
			ret_prd_info.setSv_ope_id_fk(ret_prd_info.getNx_ope_id_fk());
			ret_prd_info.setSv_ope_ver_fk(ret_prd_info.getNx_ope_ver_fk());
			ret_prd_info.setSv_proc_id_fk(ret_prd_info.getNx_proc_id_fk());
			ret_prd_info.setSv_tool_run_mode(ret_prd_info.getNx_tool_run_mode());

			ret_prd_info.setNx_path_id_fk(toPathIdFk);
			ret_prd_info.setNx_path_ver_fk(toPathVerFk);
			ret_prd_info.setNx_ope_no_fk(toOpeNoFk);
			ret_prd_info.setNx_ope_id_fk(bis_ope_to.getId().getOpe_id());
			ret_prd_info.setNx_ope_ver_fk(bis_ope_to.getId().getOpe_ver());
			ret_prd_info.setNx_proc_id_fk(bis_ope_to.getProc_id());
			ret_prd_info.setNx_tool_run_mode(bis_ope_to.getTool_run_mode());
			ret_prd_info.setRsn_cate(rsnCate);
			ret_prd_info.setRsn_code(rsnCode);
			ret_prd_info.setRsn_dept(rsnDept);
			Timestamp evt_timestamp = Util.getCurrentTimestamp();
			ret_prd_info.setEvt_timestamp(evt_timestamp);
			ret_prd_info.setEvt_usr(evtUsr);
			ret_prd_info.setEvt_cate(eveCate);
			ret_prd_info.setProc_flg(_NO);
			for (Ret_prd_info ret_prd_info1 :ret_prd_infos){
				if (ret_prd_info1.getPrd_seq_id().equals(ret_prd_info.getPrd_seq_id())){
					ret_prd_info.setProc_flg(_YES);
				}
			}
			retPrdInfoDao.update(ret_prd_info);// 更新玻璃信息
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
	}
	public void  checkPrdRepeat(String  prd_seq_id, String fr_path_id,String fr_path_ver,
								String fr_ope_no,String to_path_id,String to_path_ver,FppforceinO outTrx){//添加ret_prd_out信息时，判断是否重复
		String sql = " SELECT * FROM ret_prd_out WHERE 1=1";
		if (!Util.isSpaceCheck(prd_seq_id)) {
			sql += " AND prd_seq_id_fk='" + prd_seq_id + "'";
		}
		if (!Util.isSpaceCheck(fr_path_id)) {
			sql += " AND fr_path_id_fk='" + fr_path_id + "'";
		}
		if (!Util.isSpaceCheck(fr_path_ver)) {
			sql += " AND fr_path_ver_fk='" + fr_path_ver + "'";
		}
		if (!Util.isSpaceCheck(fr_ope_no)) {
			sql += " AND fr_ope_no_fk='" + fr_ope_no + "'";
		}
		if (!Util.isSpaceCheck(to_path_id)) {
			sql += " AND to_path_id_fk='" + to_path_id + "'";
		}
		if (!Util.isSpaceCheck(to_path_ver)) {
			sql += " AND to_path_ver_fk='" + to_path_ver + "'";
		}
		List<Ret_prd_out> ret_prd_outs = retPrdOutDao.findBySQL(sql, Ret_prd_out.class);
		if(ret_prd_outs.size()>0){
			outTrx.setRtn_mesg("玻璃代码为["+ret_prd_outs.get(0).getId().getPrd_seq_id_fk()+"]的玻璃信息已存在！！！，[Ret_prd_out]");
			outTrx.setRtn_code(E_BIS_PATHI +E_READ_NOT_FOUND +"");
			return;
		}
	}

	public SimpleRtnBeen checkBoxSwhPath(String toPathIdFk,String toPathVerFk,String toOpeNoFk,String outPathIdFk,
										 String outPathVerFk,String outOpeNoFk,FppforceinO outTrx, Bis_path bis_path_to, Bis_ope bis_ope_to){
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen();
		rtnBeen.setlRc(_NORMAL);
		/**
		 * Check To Information
		 */
		Bis_pathiId id = new Bis_pathiId();
		id.setPath_id(toPathIdFk);
		id.setPath_ver(toPathVerFk);
		Bis_pathi  bis_pathi = bisPathiDao.get(id);
		if( bis_pathi==null){
			rtnBeen.setlRc(E_BIS_PATHI +E_READ_NOT_FOUND);
			rtnBeen.setMsgBuf("卡匣分支途程不在维护中！！！,[BIS_PATHI]");
			return rtnBeen;
		}else if(bis_pathi.getFirst_ope_no()==null){
			rtnBeen.setlRc(E_BIS_OPE +E_READ_NOT_FOUND);
			rtnBeen.setMsgBuf("卡匣的分支途程的第一站不存在！！！,[BIS_OPE]");
			return rtnBeen;
		}

		Bis_pathId to_path_id = new Bis_pathId();
		to_path_id.setPath_id_fk(toPathIdFk);
		to_path_id.setPath_ver_fk(toPathVerFk);
		to_path_id.setCr_ope_no(toOpeNoFk);
		bis_path_to = bisPathDao.get(to_path_id);
		if( bis_path_to==null){
			rtnBeen.setlRc(E_BIS_PATH +E_READ_NOT_FOUND );
			rtnBeen.setMsgBuf("卡匣分支途程不存在！！！，[Bis_Path");
			return rtnBeen;
		}
		Bis_opeId  to_ope_id = new Bis_opeId();
		to_ope_id.setOpe_id(bis_path_to.getCr_ope_id_fk());
		to_ope_id.setOpe_ver(bis_path_to.getCr_ope_ver_fk());
		bis_ope_to = bisOpeDao.get(to_ope_id);
		if( bis_ope_to==null){
			rtnBeen.setlRc(E_BIS_OPE +E_READ_NOT_FOUND );
			rtnBeen.setMsgBuf("卡匣的分支站点不存在！！！，[BIS_OPE]");
			return rtnBeen;
		}
		/**
		 * Check Out Information
		 */
		Bis_pathId out_path_id = new Bis_pathId();
		out_path_id.setPath_id_fk(outPathIdFk);
		out_path_id.setPath_ver_fk(outPathVerFk);
		out_path_id.setCr_ope_no(outOpeNoFk);
		Bis_path bis_path_out = bisPathDao.get(out_path_id);
		if( bis_path_out==null){
			rtnBeen.setlRc(E_BIS_PATH +E_READ_NOT_FOUND );
			rtnBeen.setMsgBuf("卡匣的转出途程不存在！！！，[BIS_PATH]");
			return rtnBeen;
		}
		Bis_opeId  out_ope_id = new Bis_opeId();
		out_ope_id.setOpe_id(bis_path_out.getCr_ope_id_fk());
		out_ope_id.setOpe_ver(bis_path_out.getCr_ope_ver_fk());
		Bis_ope bis_ope_out   =  bisOpeDao.get(out_ope_id);
		if(bis_ope_out==null){
			rtnBeen.setlRc(E_BIS_OPE +E_READ_NOT_FOUND );
			rtnBeen.setMsgBuf("卡匣的转出站点不存在！！！，[BIS_OPE]");
			return rtnBeen;
		}
		return new SimpleRtnBeen(bis_ope_to);
	}
	public void checkBoxPrdPath(String boxId,FppforceinO outTrx){//判断箱子的NX_PATH_ID&VER信息是否维护NX_OPE_ID(bis_ope)信息是否维护
		Ret_box ret_box = retBoxDao.get(boxId);
		Bis_pathiId id = new Bis_pathiId();
		id.setPath_id(ret_box.getNx_path_id_fk());
		id.setPath_ver(ret_box.getNx_path_ver_fk());
		Bis_pathi  bis_pathi = bisPathiDao.get(id);
		if(bis_pathi==null){
			outTrx.setRtn_mesg("卡匣的下一途程不在维护中！！！，[BIS_PATHI]");
			outTrx.setRtn_code(E_BIS_PATHI +E_READ_NOT_FOUND +"");
			return;
		}
		Bis_opeId  ids = new Bis_opeId();
		ids.setOpe_id(ret_box.getNx_ope_id_fk());
		ids.setOpe_ver(ret_box.getNx_ope_ver_fk());
		Bis_ope bis_ope  =  bisOpeDao.get(ids);
		if( bis_ope==null){
			outTrx.setRtn_mesg("卡匣的下一站点不存在！！！，[BIS_OPE]");
			outTrx.setRtn_code(E_BIS_OPE +E_READ_NOT_FOUND +"");
			return;
		}

	}
	public void checkPrd(List<Ret_prd_info> ret_prd_infoList,FppforceinO outTrx){//检查卡匣里面的玻璃有没有混装
		for(Ret_prd_info ret_prd_info : ret_prd_infoList){
			if(ret_prd_info.getNx_path_id_fk().equals(ret_prd_infoList.get(0).getNx_path_id_fk())&&ret_prd_info.getNx_ope_id_fk().equals(ret_prd_infoList.get(0).getNx_ope_id_fk())){
			}else{
				outTrx.setRtn_mesg("玻璃存在混装现象");
				outTrx.setRtn_code(E_FPPFORCEIN_BOX_PRD_STATUE_ERROR+"");
				return;
			}
		}
	}
	public void checkBoxWait(Ret_box ret_box,FppforceinO outTrx){//根据boxid判断卡匣的状态
		if(!"WAIT".equals(ret_box.getBox_stat())){
			outTrx.setRtn_mesg("卡匣不在WAIT状态");
			outTrx.setRtn_code(E_FPPFORCEIN_BOX_STATUE_ERROR+"");
			return;
		}
		Bis_pathiId id = new Bis_pathiId();
		id.setPath_id(ret_box.getNx_path_id_fk());
		id.setPath_ver(ret_box.getNx_path_ver_fk());
		Bis_pathi bis_pathi = bisPathiDao.get(id);
		if(bis_pathi == null){
			outTrx.setRtn_mesg("该工艺路线["+ret_box.getNx_path_id_fk()+"]["+ret_box.getNx_path_ver_fk()+"]不存在");
			outTrx.setRtn_code(E_BIS_PATHI + E_READ_NOT_FOUND+"");
			return;
		}
		if(!bis_pathi.getPath_cate().equals(PATH_TYPE_MAIN)){
			outTrx.setRtn_mesg("该卡匣["+ret_box.getBox_id()+"]不在主工艺路线上，请确认!");
			outTrx.setRtn_code(E_BIS_PATHI + E_READ_NOT_FOUND+"");
			return;
		}
	}
	public void checkPrdWait(String boxId,FppforceinO outTrx){//根据boxid判断玻璃的状态
		List<Ret_prd_info> ret_prd_infoList = retPrdInfoDao.getShtListInfo(boxId);
		for(Ret_prd_info ret_prd_info : ret_prd_infoList){
			if(!"WAIT".equals(ret_prd_info.getPrd_stat())){
				outTrx.setRtn_mesg("玻璃代码为["+ret_prd_info.getPrd_seq_id()+"]的玻璃不是WAIT状态");
				outTrx.setRtn_code(E_FPPFORCEIN_PRD_STATUE_ERROR+"");
				return;
			}
		}
	}
	public SimpleRtnBeen deleteRetPrdOutProc(List<Ret_prd_info> ret_prd_infoList,String swh_typ,Bis_pathi crPathi){
		SimpleRtnBeen 	rtnBeen = new SimpleRtnBeen();
		rtnBeen.setlRc(_NORMAL);
		String gszMsgBuf="";
		List<Ret_prd_out>  ret_prd_outs =  new ArrayList<Ret_prd_out>();
		for(int i=0;i< ret_prd_infoList.size();i++){
			Ret_prd_info ret_prd_info = ret_prd_infoList.get(i);
			String  hql ="FROM Ret_prd_out where  prd_seq_id_fk='"+ret_prd_info.getPrd_seq_id()
					+"' and swh_typ ='"+swh_typ+"' and to_path_id_fk ='"+crPathi.getId().getPath_id()+"' and to_path_ver_fk ='"
					+crPathi.getId().getPath_ver()+"' order by evt_timestamp";
			ret_prd_outs = retPrdOutDao.find(hql);
			if(ret_prd_outs != null && ret_prd_outs.size()>0){
				retPrdOutDao.delete(ret_prd_outs.get(0));
			}
		}
		return  rtnBeen;
	}
	public void errActionFnc(FppforceinO outTrx) {
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
	}
}
